package com.stlm2.dems.service;

import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.Address;
import com.stlm2.dems.dao.AddressDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("addressService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class AddressService extends BaseServiceImpl<Address> {

	@Resource
	private AddressDao addressDao;
	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <Address> getDao() {
		return addressDao;
	}


	/**用户默认地址
	 * ffl
	 * 2017-10-18 15:12:26
	 * @param userId
	 * @return
	 */
	public   Address getByUserId(int userId){
		return  addressDao.getByUserId(userId);
	}

	/**
	 * 添加地址
	 * ffl
	 * 2017-10-18 14:59:59
	 * @param token
	 * @param city  地址城市
	 * @param location 经纬度 坐标
	 *  @param detail 详细地址
	 * @param ifDefault 是否默认  1默认 0非默认
	 * @param name   收货人姓名
	 * @param phone 收货人联系电话
	 * @return
	 */
	public String addUserAddress(String token,String city,String location,String detail,String ifDefault,String name,String phone){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if (com.stlm2.util.StringUtils.isNullOrEmpty(city)){
			return CodeContent.getModelByCode(0,"请填地址城市",null);
		}
		if(com.stlm2.util.StringUtils.isNullOrEmpty(location)){
			return CodeContent.getModelByCode(0,"请填地址经纬度",null);
		}

		if(!NumberUtils.isDigits(ifDefault)){
			return CodeContent.getModelByCode(0,"请选择是否为默认地址", null);
		}
		if (com.stlm2.util.StringUtils.isNullOrEmpty(name)){
			return CodeContent.getModelByCode(0,"请填写收货人姓名",null);
		}
		if(com.stlm2.util.StringUtils.isNullOrEmpty(phone)){
			return CodeContent.getModelByCode(0,"请填写收货人联系电话",null);
		}
		if(com.stlm2.util.StringUtils.isNullOrEmpty(detail)){
			return CodeContent.getModelByCode(0,"请填详细地址",null);
		}
		Address address2=new Address();
		address2.setUserId(user.getId());
		address2.setCity(city);
		address2.setLocation(location);
		address2.setDetail(detail.trim());
		address2.setName(name.trim());
		address2.setPhone(phone.trim());
		address2.setCreateTime(new Date());
		if(NumberUtils.toInt(ifDefault)==1){
			Address address=getByUserId(user.getId());
			if(address!=null) {
				address.setIfDefault(false);
				addressDao.modify(address);
			}
			address2.setIfDefault(true);
		}else {//设置为非默认收货地址
			List<Address> list=addressDao.getAllByUserId(user.getId());
			if(list!=null&&list.size()>0){
				Address address=getByUserId(user.getId());
				if(address!=null) {
					address2.setIfDefault(false);
				}else {//没有设置默认收货地址 设置新地址为默认收货地址
					address2.setIfDefault(true);
				}
			}else {//未添加过收货地址 设置为默认地址
				address2.setIfDefault(true);
			}
		}

		addressDao.add(address2);
		return CodeContent.getModelByCode(1,"",null);
	}

	/**
	 * 编辑用户收货地址
	 * ffl
	 * 2017-10-18 15:04:38
	 * @param token
	 * @param city  地址城市
	 * @param location 经纬度 坐标
	 *  @param detail 详细地址
	 * @param ifDefault 是否默认  1默认 0非默认
	 * @param name   收货人姓名
	 * @param phone 收货人联系电话
	 * @return
	 */
	public String editUserAddress(String token,String addressId,String city,String location,String detail,String ifDefault,String name,String phone){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		Address address2=addressDao.getById(NumberUtils.toInt(addressId));
		if(address2==null){
			return CodeContent.getModelByCode(0,"未找到要编辑的地址",null);
		}

		if(!NumberUtils.isDigits(addressId)){
			return CodeContent.getModelByCode(0,"请选择要编辑的地址", null);
		}
		if (com.stlm2.util.StringUtils.isNullOrEmpty(city)){
			return CodeContent.getModelByCode(0,"请填地址城市",null);
		}
		if(com.stlm2.util.StringUtils.isNullOrEmpty(location)){
			return CodeContent.getModelByCode(0,"请填地址经纬度",null);
		}

		if(!NumberUtils.isDigits(ifDefault)){
			return CodeContent.getModelByCode(0,"请选择是否为默认地址", null);
		}
		if (com.stlm2.util.StringUtils.isNullOrEmpty(name)){
			return CodeContent.getModelByCode(0,"请填写收货人姓名",null);
		}
		if(com.stlm2.util.StringUtils.isNullOrEmpty(phone)){
			return CodeContent.getModelByCode(0,"请填写收货人联系电话",null);
		}
		if(com.stlm2.util.StringUtils.isNullOrEmpty(detail)){
			return CodeContent.getModelByCode(0,"请填详细地址",null);
		}
		address2.setCity(city);
		address2.setLocation(location);
		address2.setDetail(detail);
		address2.setDetail(detail.trim());
		address2.setName(name.trim());
		address2.setPhone(phone.trim());
		if(NumberUtils.toInt(ifDefault)==1){
			Address address=getByUserId(user.getId());
			if(address!=null) {
				address.setIfDefault(false);
				addressDao.modify(address);
			}
			address2.setIfDefault(true);
		}else {
			address2.setIfDefault(false);
		}
		addressDao.modify(address2);
		return CodeContent.getModelByCode(1,"",null);
	}


	/**
	 * 指定地址为默认地址
	 * ffl
	 * 2017-10-18 15:03:03
	 * @param token
	 * @param addressId
	 * @return
	 */
	public String userAddressToBeDefault(String token,String addressId){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(addressId)){
			return CodeContent.getModelByCode(0,"请选择要编辑的地址", null);
		}
		Address address2=addressDao.getById(NumberUtils.toInt(addressId));
		if(address2==null){
			return CodeContent.getModelByCode(0,"未找到要编辑的地址",null);
		}
		Address address=getByUserId(user.getId());
		if(address!=null) {
			address.setIfDefault(false);
			addressDao.modify(address);
		}
		address2.setIfDefault(true);
		addressDao.modify(address2);
		return CodeContent.getModelByCode(1,"",null);
	}

	/**
	 * 删除用户地址
	 * ffl
	 * 2017-09-05 17:30:50
	 * @param token
	 * @param addressId
	 * @return
	 */
	public String deleteUserAddress(String token, String addressId){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(addressId)){
			return CodeContent.getModelByCode(0,"请选择要编辑的地址", null);
		}
		Address address=addressDao.getById(NumberUtils.toInt(addressId));
		if(address==null){
			return CodeContent.getModelByCode(0,"未找到要编辑的地址",null);
		}
		Boolean ifDefault=address.getIfDefault()!=null?address.getIfDefault():false;
		addressDao.delete(NumberUtils.toInt(addressId));

		if(ifDefault) {//若删除的地址是默认地址 指定用户其他地址中的一个为默认地址
			List<Address> addressList = addressDao.getAllByUserId(user.getId());
			if (addressList != null && addressList.size() > 0) {
				Address defaultAddress = addressList.get(0);
				defaultAddress.setIfDefault(true);
				addressDao.modify(defaultAddress);
			}
		}
		return CodeContent.getModelByCode(1,"",null);
	}

	/**
	 *  获取默认地址
	 * ffl
	 * 2017-10-18 15:05:35
	 * @param token
	 * @return
	 */
	public  String getDefault(String token){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		Address address=addressDao.getByUserId(user.getId());
		if(address==null){
			return CodeContent.getModelByCode(0,"您还没有添加收货地址，请添加收货地址",null);
		}
		return CodeContent.getModelByCode(1,"",buildModel(address));

	}


	public List<ModelMap> convert(List<Address> addressList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (addressList != null && addressList.size() > 0) {
			for (Address address : addressList) {
				ModelMap map = buildModel(address);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel(Address address) {
		ModelMap model = new ModelMap();
		if(address != null) {
			model.put("id", address.getId());
			model.put("name",address.getName()!=null?address.getName():"");
			model.put("phone",address.getPhone()!=null?address.getPhone():"");
			model.put("city", address.getCity() != null ? address.getCity() : "");
			model.put("location", address.getLocation() != null ? address.getLocation() : "");
			model.put("detail", address.getDetail() != null ? address.getDetail() : "");
			model.put("ifDefault",address.getIfDefault()!=null?address.getIfDefault():false);
		}
		return model;
	}

	/**
	 *  获取用户 所有收货地址
	 * ffl
	 * 2017-10-18 15:07:33
	 * @param token
	 * @return
	 */
	public String getUserAddress(String token){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		List<Address> list=addressDao.getAllByUserId(user.getId());
		ModelMap model=new ModelMap();
		Integer size=0;
		if(list!=null&&list.size()>0){
			size=list.size();
		}
		model.put("size",size);
		model.put("addressLest",convert(list));
		return CodeContent.getModelByCode(1,"",model);
	}


	/**
	 * 根据记录id 获取收货地址
	 * ffl
	 * 2017-11-08 14:39:26
	 * @param token
	 * @param addressId
	 * @return
	 */
	public String getAddressById( String token,String addressId){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(addressId)){
			return CodeContent.getModelByCode(0,"未传入地址id",null);
		}
		Address address=addressDao.getById(NumberUtils.toInt(addressId));
		if(address==null){
			return CodeContent.getModelByCode(0,"未找到指定id为"+addressId+"的收货地址信息",null);
		}
		if(address.getUserId()-user.getId()!=0){
			return CodeContent.getModelByCode(0,"找到指定id为"+addressId+"的收货地址信息不是您的收货地址",null);
		}
		return CodeContent.getModelByCode(1,"",buildModel(address));
	}
}